package logic

import (
	"car_srv/car"
	"car_srv/internal/svc"
	"car_srv/models"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户注册登录
func (l *UserLoginLogic) UserLogin(in *car.UserLoginRequest) (*car.UserLoginResponse, error) {
	var err error
	var user models.User
	user.Mobile = in.Mobile
	user.Password = in.Password
	if err = user.GetUserInfoById(); err != nil {
		logx.Error("查找手机号失败", err)
		return nil, err
	}

	if user.ID == 0 {
		if err = user.Create(); err != nil {
			logx.Error("用户注册失败", err)
			return nil, err
		}
	}
	return &car.UserLoginResponse{
		Id: int64(user.ID),
	}, nil
}
